/**
 * 作者: wgx
 * 日期: 2024-12-20 10:09:39
 * 备注: 全服荣誉奖励的进度值和达成的玩家
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBServerHonor;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerHonorModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id.honor_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库id
    protected int serverId; // 服务器id
    protected long honorId; // honor_type和档位组成的唯一值
    protected long userId; // 达成的玩家id

    public void reset() {
    }

    protected String getKey() {
        return "ServerHonor." + serverId + "." + honorId;
    }

    protected String getListKey() {
        return "ServerHonor." + serverId;
    }

    protected String getListItemKey() {
        return honorId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_honor"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库id
        obj.put("serverId", serverId); // 服务器id
        obj.put("honorId", honorId); // honor_type和档位组成的唯一值
        obj.put("userId", userId); // 达成的玩家id
        return obj;
    }

    public PBServerHonor.Builder takeInitBuilder() {
        PBServerHonor.Builder sb = PBServerHonor.newBuilder();
        if (honorId != 0) {
            sb.setHonorId(honorId);
        }
        if (userId != 0) {
            sb.setUserId(userId);
        }
        return sb;
    }

    public PBServerHonor.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerHonor.Builder sb = PBServerHonor.newBuilder();
        if (isFieldUpdate("honorId")) {
            sb.setHonorId(honorId);
        }
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public long getHonorId() {
        return honorId;
    }

    public void putHonorId(long honorId) {
        if (this.honorId != honorId) {
            this.honorId = honorId;
            updateRedisHashField(getKey(), "honorId", this.honorId);
        }
    }

    public void addHonorId(long value) {
        this.honorId += value;
        updateRedisHashField(getKey(), "honorId", this.honorId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

}